<?php
namespace app\demo\controller;


    use think\Request;
    use think\Controller;
    use View;
    use Db;
    use Session;
    use app\demo\validate\Login;

class Common extends Controller
{


    protected $beforeActionList = [
        'first',
    ];


    public function first(){



    }

    public function login(){


        return $this->fetch();
    }


    public function loginDo(Request $request){

        $param = $request->param();


        $rule =   [
            'username'  => 'require|alphaDash|token',
            'password'   => 'length:4,25',

        ];

        $message  =   [
            'username.require' => '用户名必须填写',
            'username.alphaDash' => '用户名格式错误',
            'username.token' => 'token错误',
            'password'     => '密码长度不符合，4-25个字符',

        ];


        $result = $this->validate($param, $rule, $message);

        //$validate= new Login();
        //$result = $validate->check($param);
        if($result === false){
            return $this->error($result);
        }


        $userInfo = Db::table('qi_user')->where('username',$param['username'])->find();


        if(empty($userInfo)){
            return $this->error('帐号不存在或密码错误!');
        }

        if($userInfo['password'] != md5($param['password'])){
            return $this->error('帐号不存在或密码错误!');
        }

        $loginInfo = [
            'username'=>$userInfo['username'],
            'remark'=>$userInfo['remark'],
        ];

        Session::set('loginInfo', $loginInfo);


        return $this->redirect('/demo/Manage/home');



    }

    public function welcome(){


        $userDatas = [
            ['name'=>'zhangsan','sex'=>'1'],
            ['name'=>'lisi','sex'=>'2'],
            ['name'=>'wangwu','sex'=>'2'],
        ];

        $this->assign('users', $userDatas);


       return $this->fetch();
    }


}